﻿
/*=========================================
* Author: VIP
* Description: 按下/抬起/进入/退出扩展事件
==========================================*/

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

//[AddComponentMenu("UI/VIPButton")]
public class VIPButton : Button,IBeginDragHandler,IDragHandler,IEndDragHandler
{

    [Serializable]
    public class VIPEvent : UnityEvent { }

    public class VIPEvent<T> : UnityEvent<T> { }

    [SerializeField]
    private VIPEvent m_onDown = new VIPEvent();

    [SerializeField]
    private VIPEvent m_onUp = new VIPEvent();

    [SerializeField]
    private VIPEvent m_onEnter = new VIPEvent();

    [SerializeField]
    private VIPEvent m_onExit = new VIPEvent();

    [SerializeField]
    private VIPEvent m_onLong = new VIPEvent();

    [SerializeField]
    private VIPEvent m_onDouble = new VIPEvent();

    [SerializeField]
    private VIPEvent m_onDragStart = new VIPEvent();

    [SerializeField]
    private VIPEvent m_onDrag = new VIPEvent();

    [SerializeField]
    private VIPEvent m_onDragEnd = new VIPEvent();

    /// <summary>
    /// 长按
    /// </summary>
    private DateTime m_longFirstTime = default(DateTime);
    private DateTime m_longSecondTime = default(DateTime);

    /// <summary>
    /// 双击
    /// </summary>
    private DateTime m_doubleFirstTime = default(DateTime);
    private DateTime m_doubleSecondTime = default(DateTime);

    public VIPEvent onDown
    {
        get { return m_onDown; }
        set { m_onDown = value; }
    }

    public VIPEvent onUp
    {
        get { return m_onUp; }
        set { m_onUp = value; }
    }

    public VIPEvent onEnter
    {
        get { return m_onEnter; }
        set { m_onEnter = value; }
    }

    public VIPEvent onExit
    {
        get { return m_onExit; }
        set { m_onExit = value; }
    }

    public VIPEvent onLong
    {
        get { return m_onLong; }
        set { m_onLong = value; }
    }

    public VIPEvent onDouble
    {
        get { return m_onDouble; }
        set { m_onDouble = value; }
    }

    public VIPEvent onDragStart {
        get { return m_onDragStart; }
        set { m_onDragStart = value; }
    }

    public VIPEvent onDrag {
        get { return m_onDrag; }
        set { m_onDrag = value; }
    }

    public VIPEvent onDragEnd {
        get { return m_onDragEnd; }
        set { m_onDragEnd = value; }
    }

    private void DownPress()
    {
        if (null != m_onDown)
            m_onDown.Invoke();
    }

    private void UpPress()
    {
        if (null != m_onUp)
            m_onUp.Invoke();
    }

    private void EnterPree()
    {
        if (null != m_onEnter)
        {
            m_onEnter.Invoke();
        }
    }

    private void ExitPree()
    {
        if (null != m_onExit)
        {
            m_onExit.Invoke();
        }
    }

    /// <summary>
    /// 长按
    /// </summary>
    private void LongPress()
    {
        if (null != onLong)
            onLong.Invoke();
        LongResetTime();
    }

    /// <summary>
    /// 双击
    /// </summary>
    private void DoublePress()
    {
        if (null != onDouble)
            onDouble.Invoke();
        DoubleResetTime();
    }

    /// <summary>
    /// 移动开始
    /// </summary>
    private void OnDragStartPress() {
        if (null != onDragStart)
            onDragStart.Invoke();
    }

    /// <summary>
    /// 移动
    /// </summary>
    private void OnDragPress()
    {
        if (null != onDrag)
            onDrag.Invoke();
    }

    /// <summary>
    /// 移动结束
    /// </summary>
    private void OnDragEndPress() {
        if (null != onDragEnd)
            onDragEnd.Invoke();
    }

    public override void OnPointerDown(PointerEventData eventData)
    {
        base.OnPointerDown(eventData);
        //按下
        DownPress();
        //长按
        if (m_longFirstTime.Equals(default(DateTime)))
            m_longFirstTime = DateTime.Now;
        //双击
        if (m_doubleFirstTime.Equals(default(DateTime)))
            m_doubleFirstTime = DateTime.Now;
        else
            m_doubleSecondTime = DateTime.Now;
    }

    public override void OnPointerUp(PointerEventData eventData)
    {
        base.OnPointerUp(eventData);
        //按下
        UpPress();
        //长按
        if (!m_longFirstTime.Equals(default(DateTime)))
            m_longSecondTime = DateTime.Now;
        if (!m_longFirstTime.Equals(default(DateTime)) && !m_longSecondTime.Equals(default(DateTime)))
        {
            var intervalTime = m_longSecondTime - m_longFirstTime;
            int milliSeconds = intervalTime.Seconds * 1000 + intervalTime.Milliseconds;
            if (milliSeconds > 600) LongPress();
            else LongResetTime();
        }
        //双击
        if (!m_doubleFirstTime.Equals(default(DateTime)) && !m_doubleSecondTime.Equals(default(DateTime)))
        {
            var intervalTime = m_doubleSecondTime - m_doubleFirstTime;
            float milliSeconds = intervalTime.Seconds * 1000 + intervalTime.Milliseconds;
            if (milliSeconds < 400)
                DoublePress();
            else
                DoubleResetTime();
        }
    }

    public override void OnPointerEnter(PointerEventData eventData)
    {
        base.OnPointerEnter(eventData);
        EnterPree();
    }

    public override void OnPointerExit(PointerEventData eventData)
    {
        base.OnPointerExit(eventData);
        ExitPree();
        //长按
        LongResetTime();
        //双击
        DoubleResetTime();
    }

    public override void OnMove(AxisEventData eventData)
    {
        base.OnMove(eventData);
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        OnDragStartPress();
    }

    public void OnDrag(PointerEventData eventData)
    {
        OnDragPress();
    }


    public void OnEndDrag(PointerEventData eventData)
    {
        OnDragEndPress();
    }

    /// <summary>
    /// 长按
    /// </summary>
    private void LongResetTime()
    {
        m_longFirstTime = default(DateTime);
        m_longSecondTime = default(DateTime);
    }

    /// <summary>
    /// 双击
    /// </summary>
    private void DoubleResetTime()
    {
        m_doubleFirstTime = default(DateTime);
        m_doubleSecondTime = default(DateTime);
    }

}
